package com.dj.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.alibaba.fastjson.JSONObject;
import com.dj.pojo.ImageMessage;
import com.dj.pojo.News;
import com.dj.pojo.NewsMessage;
import com.dj.pojo.TextMessage;
import com.thoughtworks.xstream.XStream;

public class MessageUtil {
	/** 
     * 返回消息类型：文本 
     */  
    public static final String RESP_MESSAGE_TYPE_TEXT = "text";  
  
    /** 
     * 返回消息类型：音乐 
     */  
    public static final String RESP_MESSAGE_TYPE_MUSIC = "music";  
  
    /** 
     * 返回消息类型：图文 
     */  
    public static final String RESP_MESSAGE_TYPE_NEWS = "news";  
  
    /** 
     * 请求消息类型：文本 
     */  
    public static final String REQ_MESSAGE_TYPE_TEXT = "text";  
  
    /** 
     * 请求消息类型：图片 
     */  
    public static final String REQ_MESSAGE_TYPE_IMAGE = "image";  
  
    /** 
     * 请求消息类型：链接 
     */  
    public static final String REQ_MESSAGE_TYPE_LINK = "link";  
  
    /** 
     * 请求消息类型：地理位置 
     */  
    public static final String REQ_MESSAGE_TYPE_LOCATION = "location";  
  
    /** 
     * 请求消息类型：音频 
     */  
    public static final String REQ_MESSAGE_TYPE_VOICE = "voice";  
  
    /** 
     * 请求消息类型：推送 
     */  
    public static final String REQ_MESSAGE_TYPE_EVENT = "event";  
  
    /** 
     * 事件类型：subscribe(订阅) 
     */  
    public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";  
  
    /** 
     * 事件类型：unsubscribe(取消订阅) 
     */  
    public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";  
  
    /** 
     * 事件类型：CLICK(自定义菜单点击事件) 
     */  
    public static final String EVENT_TYPE_CLICK = "CLICK";

	/**
	 * xml转换为map
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException {
		Map<String, String> map = new HashMap<String, String>();
		SAXReader reader = new SAXReader();
		InputStream ins = null;
		try {
			ins = request.getInputStream();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		Document doc = null;
		try {
			// 读取XML文档
			doc = reader.read(ins);
			Element root = doc.getRootElement();
			List<Element> list = root.elements();
			// 遍历子节点到map中
			for (Element e : list) {
				map.put(e.getName(), e.getText());
			}
			System.out.println(JSONObject.toJSON(map));
			return map;
		} catch (DocumentException e1) {
			e1.printStackTrace();
		} finally {
			ins.close();
		}
		return null;
	}

	/**
	 * 将文本消息对象转化成XML格式
	 * @param message 	文本消息对象
	 * @return 返回转换后的XML格式
	 */
	public static String objectToXml(TextMessage message) {
		XStream xs = new XStream();
		// 由于转换后xml根节点默认为class类，需转化为<xml>
		xs.alias("xml", message.getClass());
		return xs.toXML(message);
	}
	
	/**
	 * 图片消息转xml
	 * @param imageMessage
	 * @return
	 */
	public static String imageMessageXml(ImageMessage imageMessage) {
		XStream xs = new XStream();
		// 由于转换后xml根节点默认为class类，需转化为<xml>
		xs.alias("xml", imageMessage.getClass());
		return xs.toXML(imageMessage);
	}
	
	/**
     * 将图文消息对象转成XML
     * @param 
     * @return
     */
    public static String newsMessageToXml(NewsMessage newsMessage){
        XStream xstream = new XStream();
        //将xml的根节点替换成<xml>  默认为NewsMessage的包名
        xstream.alias("xml", newsMessage.getClass());
        //同理，将每条图文消息News类的报名，替换为<item>标签
        xstream.alias("item", new News().getClass());
        return xstream.toXML(newsMessage);
    }
}
